package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketCouponServiceImpl implements MarketCouponService {
    @Override
    public List<MarketCoupon> list(Integer limit, Integer page, String name, Short type, Short status, String sort, String order) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.setOrderByClause(sort + " " + order);
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        if(status!=null){
            criteria.andStatusEqualTo(status);
        }
        PageHelper.startPage(page, limit);
        List<MarketCoupon> marketCoupons = mapper.selectByExampleSelective(marketCouponExample, MarketCoupon.Column.id, MarketCoupon.Column.name, MarketCoupon.Column.desc,
                MarketCoupon.Column.tag, MarketCoupon.Column.total, MarketCoupon.Column.discount, MarketCoupon.Column.min, MarketCoupon.Column.limit,
                MarketCoupon.Column.type, MarketCoupon.Column.status, MarketCoupon.Column.goodsType, MarketCoupon.Column.goodsValue, MarketCoupon.Column.timeType,
                MarketCoupon.Column.days, MarketCoupon.Column.addTime, MarketCoupon.Column.updateTime, MarketCoupon.Column.deleted);
        session.commit();
        session.close();
        return marketCoupons;
    }

    @Override
    public void update(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        session.commit();
        session.close();
    }

    @Override
    public void create(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        marketCouponMapper.insert(marketCoupon);
        session.commit();
        session.close();
    }

    @Override
    public void delete(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
        session.commit();
        session.close();
    }

    @Override
    public MarketCoupon read(int i) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = mapper.selectByPrimaryKey(i);
        session.commit();
        session.close();
        return marketCoupon;
    }
}
